import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;


public class ModeChooser extends JFrame {
	private JRadioButton host, connect;
	private JButton start;
	private JTextField hostname, port, name;
	ModeChooser thisChooser = this; 
	public ModeChooser() {
		super("NetworkCardTable: New game");
		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		JLabel label = new JLabel("Please choose an option:");
		host = new JRadioButton("Host new game");
		connect = new JRadioButton("Connect to game");
		ButtonGroup options = new ButtonGroup();
		options.add(host);
		options.add(connect);
		host.addActionListener(new ModeListener());
		connect.addActionListener(new ModeListener());
		host.setSelected(true);
		JLabel lHostname = new JLabel("Hostname:");
		hostname = new JTextField("localhost");
		hostname.setEnabled(false);
		JLabel lName = new JLabel("Your name:");
		name = new JTextField("YourName");
		JLabel lPort = new JLabel("Port:");
		port = new JTextField("6666");
		start = new JButton("Start");
		start.addActionListener(new ModeListener());
		panel.add(label);
		panel.add(host);
		panel.add(connect);
		JPanel pHost = new JPanel();
		pHost.setLayout(new BoxLayout(pHost, BoxLayout.X_AXIS));
		pHost.add(lHostname);
		pHost.add(hostname);
		pHost.setAlignmentX(LEFT_ALIGNMENT);
		JPanel pName = new JPanel();
		pName.setLayout(new BoxLayout(pName, BoxLayout.X_AXIS));
		pName.setAlignmentX(LEFT_ALIGNMENT);
		pName.add(lName);
		pName.add(name);
		pName.add(lPort);
		pName.add(port);
		panel.add(pHost);
		panel.add(pName);
		panel.add(start);
		add(panel);
		pack();
		setVisible(true);
	}
	public static void main(String args[]) {
		ModeChooser choose = new ModeChooser();
	}
	class ModeListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == connect)
				hostname.setEnabled(true);
			else if (e.getSource() == host) {
				hostname.setEnabled(false);
				hostname.setText("localhost");
			} else if (e.getSource() == start) {
				new Game(hostname.getText(), Integer.parseInt(port.getText()), name.getText(), host.isSelected());
				thisChooser.setVisible(false);
			}
			
		}
		
	}
}
